package com.daohang361.imqq.ui;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;

import com.csu.client.module.vo.Cache;
import com.csu.client.module.vo.MsgFriendGroup;
import com.csu.client.module.vo.MsgUser;
import com.csu.client.tools.EIMClientConfig;
import com.daohang361.imqq.resourse.ResourcesManagement;
import com.daohang361.imqq.ui.autoCompletionField.AutoCompletionField;
import com.daohang361.imqq.ui.autoCompletionField.DefaultCompletionFilter;
import com.daohang361.imqq.ui.headtree.TreeScrollPane;
import com.daohang361.imqq.ui.snapTipTabbedPane.SnapTipTabbedPane;
import com.daohang361.imqq.ui.trayIcon.EIMTrayIcon;


public class EIMMainFrame extends BaseFrame {
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static List<MsgFriendGroup> friends=new ArrayList<MsgFriendGroup>();
	private static MsgUser ower=new MsgUser();
	
	private static EIMMainFrame instance;
	public static EIMMainFrame getInstance(){
           if(instance==null){
        	   instance=new EIMMainFrame();
           }
           return instance;
	}
	
	private EIMMainFrame() {
        initComponents();
    }
	
    public  void changTryIcon(){
    	EIMTrayIcon qqTrayIcon=EIMTrayIcon.getInStance();
    	qqTrayIcon.setFrame(this);
    	qqTrayIcon.setTrayIconState(EIMClientConfig.OnlineTryIcon_Type);
    	qqTrayIcon.showIcon(EIMClientConfig.OnlineTryIcon_Type);
    } 
    
    private void initComponents() {
        this.setSize(340, 600);
        this.setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(340, 400));
        jPanel1 = new javax.swing.JPanel();
        
        findField = new AutoCompletionField();
        Cache cache=Cache.getInstance();
        findField.setFilter(new DefaultCompletionFilter(cache.getfriendMap()));
        
        headImage = new javax.swing.JLabel();
        stateComboBox = new javax.swing.JComboBox();
        personWord = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        
        compnayHome = new javax.swing.JButton();
        Icon compnayhomIcon=ResourcesManagement.getImageIcon("ui/compnayHome.gif",16,16);
        compnayHome.setIcon(compnayhomIcon);
        
        compnayEmail = new javax.swing.JButton();
        Icon compnayEmailIcon=ResourcesManagement.getImageIcon("ui/Explorer.png",16,16);
        compnayEmail.setIcon(compnayEmailIcon);
        
        compnayOA = new javax.swing.JButton();
        
        Icon oaIcon=ResourcesManagement.getImageIcon("ui/oa.gif",16,16);
        compnayOA.setIcon(oaIcon);
        
        news = new javax.swing.JButton();
        Icon newsIcon=ResourcesManagement.getImageIcon("ui/news.gif",16,16);
        news.setIcon(newsIcon);
        
        jPanel2 = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        iEButton = new javax.swing.JButton();
        Icon iEIcon=ResourcesManagement.getImageIcon("ui/Explorer_16.png",16,16);
        iEButton.setIcon(iEIcon);
        personHomeButton = new javax.swing.JButton();
        Icon homIcon=ResourcesManagement.getImageIcon("ui/Home.png",16,16);
        personHomeButton.setIcon(homIcon);
        
        mySetButton = new javax.swing.JButton();
        Icon mySetIcon=ResourcesManagement.getImageIcon("ui/set.gif",16,16);
        mySetButton.setIcon(mySetIcon);
        sysSetButton = new RButton(RButton.RIGHT_ROUND_RECT);
        jToolBar3 = new javax.swing.JToolBar();
        infoButton = new javax.swing.JButton();
        findButton = new javax.swing.JButton();
        Icon infoIcon=ResourcesManagement.getImageIcon("ui/sysIfo.gif");
        infoButton.setIcon(infoIcon);
        Icon findIcon=ResourcesManagement.getImageIcon("ui/search.gif");
        findButton.setIcon(findIcon);
       
        mainTabbedPanel = new SnapTipTabbedPane();
        friendPanel = new javax.swing.JPanel();
        friendTree=new TreeScrollPane(friends);
        teamPanel = new javax.swing.JPanel();
        zuijinPanel = new javax.swing.JPanel();

        jToolBar1.setEnabled(false);
        jToolBar2.setEnabled(false);
        jToolBar3.setEnabled(false);

        headImage.setBackground(new java.awt.Color(255, 255, 255));
        headImage.setForeground(new java.awt.Color(255, 255, 102));
        
        String headnum=(String)ower.getAttribute("headImage");
        Icon headIcon=ResourcesManagement.getImageIcon("head/"+headnum+".gif");
        
        headImage.setIcon(headIcon);
        headImage.setMinimumSize(new java.awt.Dimension(42, 15));

        stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "我在线上", "Q我吧", "忙碌", "隐身" }));

        personWord.setText(ower.getPersonWord());

        compnayHome.setText("公司首页");
        jToolBar1.add(compnayHome);

        compnayEmail.setText("公司邮箱");
        jToolBar1.add(compnayEmail);

        compnayOA.setText("公司OA");
        jToolBar1.add(compnayOA);

        news.setText("公司新闻");
        jToolBar1.add(news);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(stateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(personWord, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
            .addComponent(findField, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(headImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(stateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(personWord)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(findField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );

        jToolBar2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("\u5feb\u6377")));
        iEButton.setText("浏览器");
        jToolBar2.add(iEButton);

        personHomeButton.setText("个人主页");
        jToolBar2.add(personHomeButton);

        mySetButton.setText("我的设置");
        jToolBar2.add(mySetButton);

        sysSetButton.setText("jButton5");

        infoButton.setText("信息");
        jToolBar3.add(infoButton);

        findButton.setText("查找");
        jToolBar3.add(findButton);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(sysSetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar3, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToolBar3, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                    .addComponent(sysSetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout friendPanelLayout = new javax.swing.GroupLayout(friendPanel);
        friendPanel.setLayout(friendPanelLayout);
        friendPanelLayout.setHorizontalGroup(
                friendPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(friendTree, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
            );
            friendPanelLayout.setVerticalGroup(
                friendPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(friendTree, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
            );
        mainTabbedPanel.addTab("好友列表", friendPanel);

        javax.swing.GroupLayout teamPanelLayout = new javax.swing.GroupLayout(teamPanel);
        teamPanel.setLayout(teamPanelLayout);
        teamPanelLayout.setHorizontalGroup(
            teamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        teamPanelLayout.setVerticalGroup(
            teamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        mainTabbedPanel.addTab("群列表", teamPanel);

        javax.swing.GroupLayout zuijinPanelLayout = new javax.swing.GroupLayout(zuijinPanel);
        zuijinPanel.setLayout(zuijinPanelLayout);
        zuijinPanelLayout.setHorizontalGroup(
            zuijinPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        zuijinPanelLayout.setVerticalGroup(
            zuijinPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        mainTabbedPanel.addTab("最近联系人", zuijinPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(mainTabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainTabbedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        addWindowListener(new WindowAdapter(){
	    	public void windowClosing(WindowEvent e){
	    	   	setVisible(false);
	    	}});
        this.setMainColor(new java.awt.Color(102, 204, 255));
        this.setAlwaysOnTop(true);
    }                  
    /**
     * 利用从TestEIMTree中的数据初始化列表
     * @param groupList
     */
    public void initMainTree(List<MsgFriendGroup> groupList){
    	TreeScrollPane treeScrollPane=new TreeScrollPane(groupList);
    	friendTree=treeScrollPane;
    	this.repaint();
    }
    public void setMainColor(Color bg){
    	mainTabbedPanel.setBackground(bg);
    	friendPanel.setBackground(bg);
    	teamPanel.setBackground(bg);
    	zuijinPanel.setBackground(bg);
    	jPanel1.setBackground(bg);
    	jPanel2.setBackground(bg);
    	jToolBar1.setBackground(bg);
    	jToolBar2.setBackground(bg);
    	jToolBar3.setBackground(bg);
    	jToolBar1.setForeground(bg);
    	jToolBar2.setForeground(bg);
    	jToolBar3.setForeground(bg);
    	
    }

	public static List<MsgFriendGroup> getFriends() {
		return friends;
	}

	public static void setFriends(List<MsgFriendGroup> friends) {
		EIMMainFrame.friends = friends;
	}

	public static MsgUser getOwer() {
		return ower;
	}

	public static void setOwer(MsgUser ower) {
		EIMMainFrame.ower = ower;
	}
    
	public TreeScrollPane getFriendTree() {
		return friendTree;
	}

	public void setFriendTree(TreeScrollPane friendTree) {
		this.friendTree = friendTree;
	}
    
    // 变量声明 - 不进行修改                     
    private javax.swing.JButton compnayEmail;
    private javax.swing.JButton compnayHome;
    private javax.swing.JButton compnayOA;
    private javax.swing.JButton findButton;
    private AutoCompletionField findField;
    private javax.swing.JPanel friendPanel;
    private TreeScrollPane friendTree;
    private javax.swing.JLabel headImage;
    private javax.swing.JButton iEButton;
    private javax.swing.JButton infoButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private SnapTipTabbedPane mainTabbedPanel;
    private javax.swing.JButton mySetButton;
    private javax.swing.JButton news;
    private javax.swing.JButton personHomeButton;
    private javax.swing.JLabel personWord;
    private javax.swing.JComboBox stateComboBox;
    private RButton sysSetButton;
    private javax.swing.JPanel teamPanel;
    private javax.swing.JPanel zuijinPanel;
    // 变量声明结束                   

}